package indi.a709924470.scpwiki.RecycleViewThings;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import indi.a709924470.scpwiki.HolderType;
import indi.a709924470.scpwiki.R;
import indi.a709924470.scpwiki.beans.WikiItems;

/**
 * Created by Walter Ji on 2017/12/21.
 */

public class rcvwHolder extends RecyclerView.Adapter<rcvwHolder.RVHlder> {
    private List<WikiItems> data;
    private Context ctx;
    private Boolean clicked = false;
    private int type;

    public rcvwHolder(List<WikiItems> lw, Context ctx, int type){
        this.data = lw;
        this.ctx = ctx;
        this.type = type;
    }

    @Override
    public rcvwHolder.RVHlder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RVHlder(LayoutInflater.from(ctx).inflate(R.layout.wiki_items, parent, false));
    }

    @Override
    public void onBindViewHolder(final RVHlder holder, int position) {
        holder.tv.setText(data.get(position).name);
        if (data.get(position).name == null){
            holder.tv.setVisibility(View.GONE);
        }
        holder.tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!clicked) {
                    holder.subrv.setVisibility(View.GONE);
                    clicked = true;
                }else{
                    holder.subrv.setVisibility(View.VISIBLE);
                    clicked = false;
                }
            }
        });
        srcvwHolder sh = new srcvwHolder(data.get(position).getWsi(),ctx,type);
        holder.subrv.setAdapter(sh);
        holder.subrv.setLayoutManager(new LinearLayoutManager(ctx));
        holder.subrv.addItemDecoration(new itemDecorate(ctx, LinearLayout.HORIZONTAL));
        sh.notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {return data == null ? 0 : data.size();}

    class RVHlder extends RecyclerView.ViewHolder {
        TextView tv;
        RecyclerView subrv;
        public RVHlder(View iV){
            super(iV);
            tv = (TextView) iV.findViewById(R.id.itemName);
            subrv = (RecyclerView) iV.findViewById(R.id.subRV);
        }
    }
}
